package com.rexense.beacon;

import Jama.Matrix;
import com.rexense.beacon.alg.BleBase;

import java.util.Arrays;
import java.util.List;

public class X {

    public static void main(String[] args) {
    }


    public static void ca(List<BleBase> baseList) {
        int len = baseList.size();

        double[][] a = new double[len - 1][2];
        double[][] b = new double[len - 1][1];

        BleBase bleBaseN = baseList.get(len - 1);
        for (int i = 0; i < len - 1; i++) {
            BleBase bleBaseI = baseList.get(i);
            a[i][0] = 2 * (bleBaseI.getX() - bleBaseN.getX());
            a[i][1] = 2 * (bleBaseI.getY() - bleBaseN.getY());

            b[i][0] = Math.pow(bleBaseI.getX(), 2) - Math.pow(bleBaseN.getX(), 2)
                    + Math.pow(bleBaseI.getY(), 2) - Math.pow(bleBaseN.getY(), 2)
                    + Math.pow(bleBaseN.getDistinct(), 2) - Math.pow(bleBaseI.getDistinct(), 2);
        }

        Matrix matrixA = new Matrix(a);
        Matrix matrixB = new Matrix(b);

        Matrix matrixAT = matrixA.transpose();

        Matrix timesAtA = matrixAT.times(matrixA);

        //防止出现奇异矩阵无法运算
//        while (timesAtA.det() == 0.) {
//            double[][] array = timesAtA.getArray();
//            for (int i = 0; i < array.length; i++) {
//                for (int j = 0; j < array[0].length; j++) {
//                    if (array[i][j] == 0.) {
//                        array[i][j] += 0.0000000001;
//                    }
//                }
//            }
//        }
        Matrix timesAtAInverse = timesAtA.inverse();

        Matrix timesAtAInverseAT = timesAtAInverse.times(matrixAT);

        Matrix timesAtAInverseATB = timesAtAInverseAT.times(matrixB);

        double[][] array = timesAtAInverseATB.getArray();
        System.out.println(Arrays.deepToString(array));
    }

}
